<template>
  <!-- 登录的页面 -->
  <div class="login_container">
    <!-- 登录 的form表单 -->
    <div class="login_box">

      <!-- 头像 -->
      <div class="avatar_box">
        <img src="../../assets/imgs/tx.png"
             alt="">
      </div>
      <!-- 登录的表单组件 -->
      <el-form :model="loginForm"
               ref="loginForm"
               :rules="loginRules"
               label-width="0px"
               class="login_form">
        <el-form-item prop="cardNo">
          <el-input v-model="loginForm.cardNo"
                    placeholder="请输入用户名称"
                    prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password"
                    placeholder="请输入登录密码"
                    prefix-icon="el-icon-lock"></el-input>
        </el-form-item>

        <el-form-item class="login_btn">
          <el-button type="primary"
                     @click="submitForm('loginForm')">登录</el-button>
          <el-button @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from '../../api/user'
import { mapMutations } from 'vuex'
import { USERLOGIN, USERLOGOUT } from '../../store/types'
export default {
  data () {
    return {
      loginForm: {
        cardNo: '1',
        password: '110'
      },
      loginRules: {
        cardNo: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' },
        ],
      }
    }
  },
  methods: {
    ...mapMutations([USERLOGIN, USERLOGOUT]),
    submitForm (formName) {
      this.$refs[formName].validate(async (valid) => {
        //校验通过 进行登录
        if (valid) {
          //执行登录
          let result = await login(this.loginForm.cardNo, this.loginForm.password);

          if (result.token != "") {
            var cardNo = this.loginForm.cardNo;
            var pwd = this.loginForm.password;
            // 保存token用于权限验证
            this[USERLOGIN](result.token);
            this.$message({
              message: '登录成功',
              type: 'success',
              center: true,
              showClose: true
            });
            localStorage.setItem('cardNo', cardNo);
            localStorage.setItem('password', pwd);
            this.$router.push("home")
          } else {
            this.$message({
              message: '账号或密码错误',
              center: true,
              type: 'error',
              showClose: true
            });
            return false;
          }

        }
      });
    },
    resetForm (formName) {

    }
  }

}
</script>

<style lang="less">
.login_container {
  height: 100%;
  background-color: aquamarine;
  display: flex;
  justify-content: center;
  align-items: center;

  .login_box {
    width: 420px;
    height: 260px;
    background-color: #fff;
    border-radius: 5px;
    text-align: center;
    padding: 30px;

    .login_form {
      position: relative;
      top: -80px;

      .verifyCode_img {
        position: relative;
        top: -6px;
      }
    }

    .verifyCode_box {
      display: flex;
      .verifyCode {
        flex: 2;
      }
      img {
        flex: 1;
      }
    }
  }

  .avatar_box {
    width: 130px;
    height: 130px;
    border-radius: 75px;
    margin-bottom: 20px;
    overflow: hidden;
    position: relative;
    left: 138px;
    top: -80px;
    background-color: #fff;
    padding: 8px;

    img {
      height: 100%;
      border-radius: 50%;
      width: 100%;
    }
  }
}
</style>